#include "SceneNode.h"

namespace gameCG {
	SceneNode::SceneNode(SceneElement* elem) {
		m_elem = elem;
		m_numChildren = 0;
	}

	SceneNode::~SceneNode() {
		delete m_elem;
		for(int i=0; i < m_numChildren; i++) {
			delete m_children[i];
		}
	}

	void SceneNode::render() {
		if(m_elem == NULL)
			return;

		glMatrixMode(GL_MODELVIEW);
		
		glPushMatrix();
			m_elem->applyTransformations();
			m_elem->draw();
			
			for(int i=0; i < m_numChildren; i++) {
				m_children[i]->render();
			}
		glPopMatrix();
	}

 	void SceneNode::update(int delta) {		
 		if(m_elem == NULL)
 			return;
 
 		m_elem->update(delta);
 
 		for(int i=0; i < m_numChildren; i++) {
 				m_children[i]->update(delta);
 		}
 	}

	int SceneNode::addChild(SceneNode* child) {
		if(child == NULL)
			return -1;

		m_children[m_numChildren] = child;
		return m_numChildren++;
	}

	SceneNode* SceneNode::getChild(int i) {
		if((i < 0 ) || (i > m_numChildren))
			return NULL;
	
		return m_children[i];
	}

	SceneElement* SceneNode::getElement() {
		return m_elem;
	}
}
